package book;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Book implements Comparable<Book>{
    private int bookId;           //书id
    private String title;         // 书名
    private String author;        // 作者
    private String category;      // 类别
    private int publishYear;      // 出版年份
    private boolean borrowedStatus;   // 借阅状态
    private int borrowCount;      // 借阅次数
    private LocalDate shelfDate;       // 上架时间

    public Book(String title, String author, String category, int publishYear, LocalDate shelfDate) {
        this.title = title;
        this.author = author;
        this.category = category;
        this.publishYear = publishYear;
        this.shelfDate = shelfDate;
    }

    public int getBookId() {
        return bookId;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getCategory() {
        return category;
    }

    public int getPublishYear() {
        return publishYear;
    }

    public boolean getBorrowedStatus() {
        return borrowedStatus;
    }

    public int getBorrowCount() {
        return borrowCount;
    }

    public LocalDate getShelfDate() {
        return shelfDate;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public void setBookId(int bookId) {
        this.bookId = bookId;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public void setPublishYear(int publishYear) {
        this.publishYear = publishYear;
    }

    public void setBorrowedStatus(boolean borrowed) {
        borrowedStatus = borrowed;
    }

    public void setBorrowCount(int borrowCount) {
        this.borrowCount = borrowCount;
    }

    public void setShelfDate(LocalDate shelfDate) {
        this.shelfDate = shelfDate;
    }

    public void incrementBorrowCount() {
        this.borrowCount++;
    }

    public void decreaseBorrowCount() {
        this.borrowCount--;
    }

    public void decreaseBookId() {
        this.bookId--;
    }

    @Override
    public String toString() {
        return "Book{ " +
                "图书Id=" + bookId +
                ", 名字='" + title + '\'' +
                ", 作者='" + author + '\'' +
                ", 类别='" + category + '\'' +
                ", 出版年份=" + publishYear +
                ", 借阅状态=" + borrowedStatus +
                ", 借阅次数=" + borrowCount +
                ", 上架时间=" + shelfDate +
                " }";
    }

    @Override
    public int compareTo(Book o) {
        return o.borrowCount - this.borrowCount;
    }

    public String toJSON() {
        StringBuilder json = new StringBuilder();
        json.append(bookId + ",");
        json.append(title + ",");
        json.append(author + ",");
        json.append(category + ",");
        json.append(publishYear + ",");
        json.append(borrowedStatus + ",");
        json.append(borrowCount + ",");
        //将 localData 类转化为字符串类型拼接起来
        //排除上架时间为空情况
        json.append(shelfDate != null ? shelfDate.format(DateTimeFormatter.ISO_LOCAL_DATE) : "null");
        return json.toString();
    }
}
